package com.cipta.lomob.fragments;

import it.sephiroth.android.library.widget.AdapterView;
import it.sephiroth.android.library.widget.HListView;

import java.util.ArrayList;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;

import com.cipta.lomob.R;
import com.cipta.lomob.VideoDetailActivity;
import com.cipta.lomob.adapter.AdapterHomeTimeline;
import com.cipta.lomob.adapter.PopularVideoFragmentAdapter;
import com.cipta.lomob.base.BaseFragment;
import com.cipta.lomob.model.ItemVideo;
import com.cipta.lomob.utils.SampleData;

public class HomeFragment extends BaseFragment {
	private ViewPager pager;
	private HListView lvTrending, lvBroadcast, lvRecent;
	
	private ArrayList<ItemVideo> listPopular, listTrending, listBroadcast, listRecent;
	
	public HomeFragment(){}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
		setHasOptionsMenu(true);
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        initializeViews(rootView);
        return rootView;
    }
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initializeLibs();
		
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		initializeProcess();
	}
	
	@Override
	public void initializeLibs() {
		// TODO Auto-generated method stub
		super.initializeLibs();
		
		listBroadcast = new ArrayList<ItemVideo>();
		listPopular = new ArrayList<ItemVideo>();
		listRecent = new ArrayList<ItemVideo>();
	} 
	
	@Override
	public void initializeViews(View view) {
		// TODO Auto-generated method stub
		super.initializeViews(view);
		
		lvTrending = (HListView)view.findViewById(R.id.lv_home_timeline_trending);
		lvBroadcast = (HListView)view.findViewById(R.id.lv_home_timeline_broadcast);
		lvRecent = (HListView)view.findViewById(R.id.lv_home_timeline_recent);
		
		pager = (ViewPager)view.findViewById(R.id.pager_popular_video);
		pager.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				pager.getParent().requestDisallowInterceptTouchEvent(true);
				return false;
			}
		});
	}
	
	@Override
	public void initializeProcess() {
		// TODO Auto-generated method stub
		super.initializeProcess();
		
		listBroadcast = ItemVideo.getListVideo(SampleData.sampleDataBroadcast, SampleData.sampleImageBroadcast);
		listPopular = ItemVideo.getListVideo(SampleData.sampleDataPopularVideo, SampleData.sampleImagePopularVideo);
		listRecent = ItemVideo.getListVideo(SampleData.sampleDataRecent, SampleData.sampleImageRecent);
		listTrending = ItemVideo.getListVideo(SampleData.sampleDataTrending, SampleData.sampleImageTrending);
		
		PopularVideoFragmentAdapter popularVideoFragmentAdapter = new PopularVideoFragmentAdapter(getChildFragmentManager(), listPopular);
		pager.setAdapter(popularVideoFragmentAdapter);
		
		AdapterHomeTimeline adapterHomeTimeline = new AdapterHomeTimeline(getActivity(), listTrending);
		lvTrending.setAdapter(adapterHomeTimeline);
		lvTrending.setOnItemClickListener(videoItemClickListener);
		
		AdapterHomeTimeline adapterBroadcast = new AdapterHomeTimeline(getActivity(), listBroadcast);
		lvBroadcast.setAdapter(adapterBroadcast);
		lvBroadcast.setOnItemClickListener(videoItemClickListener);
		
		AdapterHomeTimeline adapterRecent = new AdapterHomeTimeline(getActivity(), listRecent);
		ItemVideo video = (ItemVideo) adapterRecent.getItem(0);
		lvRecent.setAdapter(adapterRecent);
		lvRecent.setOnItemClickListener(videoItemClickListener);
		
	}
	
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.menu_home, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// toggle nav drawer on selecting action bar app icon/title
		
		// Handle action bar actions click
		switch (item.getItemId()) {
		case R.id.action_search:
			
			return true;
		case R.id.action_broadcast:
			
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	private void showInetConnectionDialog(){
		final Dialog dialog = new Dialog(getActivity());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_internet_connection);
		Button btnOk = (Button)dialog.findViewById(R.id.btn_dialog_inet_);
		btnOk.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
			}
		});
		dialog.show();
	}
	
	private AdapterView.OnItemClickListener videoItemClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> adapter, View view, int index,
				long time) {
			ItemVideo video = (ItemVideo) adapter.getItemAtPosition(index);
			VideoDetailActivity.showVideo(getActivity(), video);
		}
	};
}
